#include "hexreader.h"


uint32_t readLONG(std::vector<uint8_t> arr,int startposion,bool dir)
{
    if(dir)
    {
        uint32_t retval = arr[startposion + 0];
        for(int i = 1;i<4;i++)
        {
            uint32_t middle = arr[startposion + i];
            retval = retval | (middle << (4 * i));
        }
        return retval;
    }
    else
    {
        uint32_t retval = arr[startposion + 3];
        for(int i = 1;i<4;i++)
        {
            uint32_t middle = arr[startposion + 3 - i];
            retval = retval | (middle << (4 * i));
        }
        return retval;
    }
}

uint32_t readLONG(uint8_t arr[4],bool dir)
{
    if(dir)
    {
        uint32_t retval = arr[0];
        for(int i = 1;i<4;i++)
        {
            uint32_t middle = arr[i];
            retval = retval | (middle << (4 * i));
        }
        return retval;
    }
    else
    {
        uint32_t retval = arr[3];
        for(int i = 1;i<4;i++)
        {
            uint32_t middle = arr[3 - i];
            retval = retval | (middle << (4 * i));
        }
        return retval;
    }
}

uint32_t readDWORD(std::vector<uint8_t> arr,int startposion,bool dir)
{
    if(dir)
    {
        uint32_t retval = arr[startposion + 0];
        for(int i = 1;i<4;i++)
        {
            uint32_t middle = arr[startposion + i];
            retval = retval | (middle << (4 * i));
        }
        return retval;
    }
    else
    {
        uint32_t retval = arr[startposion + 3];
        for(int i = 1;i<4;i++)
        {
            uint32_t middle = arr[startposion + 3 - i];
            retval = retval | (middle << (4 * i));
        }
        return retval;
    }
}

uint32_t readDWORD(uint8_t arr[4],bool dir)
{
    
    if(dir)
    {
        uint32_t retval = arr[0];
        for(int i = 1;i<4;i++)
        {
            uint32_t middle = arr[i];
            retval = retval | (middle << (4 * i));
        }
        return retval;
    }
    else
    {
        uint32_t retval = arr[3];
        for(int i = 1;i<4;i++)
        {
            uint32_t middle = arr[3 - i];
            retval = retval | (middle << (4 * i));
        }
        return retval;
    }
}

uint16_t readWORD(std::vector<uint8_t> arr, int startposion,bool dir)
{
    uint16_t retval = 0x0000;
    if(dir)
    {
        retval = arr[startposion + 0];
        retval = retval | (static_cast<uint16_t>(arr[startposion + 1]) << 4);
    }
    else 
    {
        retval = arr[startposion + 1];
        retval = retval | (static_cast<uint16_t>(arr[startposion + 0]) << 4);
    }
    return retval;
}

uint16_t readWORD(uint8_t arr[2],bool dir)
{
    uint16_t retval = 0x0000;
    if(dir)
    {
        retval = arr[0];
        retval = retval | (static_cast<uint16_t>(arr[1]) << 4);
    }
    else 
    {
        retval = arr[1];
        retval = retval | (static_cast<uint16_t>(arr[0]) << 4);
    }
    return retval;
}